//
//  DetailContentCell.swift
//  CodingTest
//
//  Created by LYPC on 2021/11/20.
//

import UIKit

extension DetailContentCell: CTTableViewCellInitProtocol { }

class DetailContentCell: UITableViewCell {
    
    private var model: DataModel?
    
    private let contentL = UILabel() /// 与link公用
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupLayout()
        setupStyle()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

// MARK: - Reactive
extension DetailContentCell: ViewModelConfigurable {
    
    func configure(_ model: DataModel) {
        self.model = model
        contentL.isHidden = model.content.isEmpty
        contentL.text = model.content
        
        updateLayout(model)
    }
}

// MARK: - UI Setup
private extension DetailContentCell {
    func setupLayout() {
        clipsToBounds = true
        contentView.addSubview(contentL)
        contentL.snp.makeConstraints { (m) in
            m.leading.trailing.top.equalToSuperview().inset(20.fitR)
            m.height.equalTo(0)
        }
    }
    
    func updateLayout(_ model: DataModel) {
        if !model.content.isEmpty {
            contentL.snp.updateConstraints { (m) in
                m.height.equalTo(model.contentLHeight_detail)
            }
        }
    }
    
    func setupStyle() {
        selectionStyle = .none
        backgroundColor = .color(.white)
        contentL.do { (v) in
            v.numberOfLines = 0
            v.font = .regular(16)
            v.textColor = .color(.primary)
            v.backgroundColor = .color(.white)
            v.isHidden = true
        }
    }
}

